package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.List;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.data.Affix;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.fragment.CreatView;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WordInnerPagerAdapter extends PagerAdapter {

	private Context mContext;
	private OneWord mOneWord;
	private boolean testMode;
	private boolean showAffix;
	private boolean showSameShape;
	private View views[] = new View[3];
	private int initPageIndex = 0;

	public WordInnerPagerAdapter(Context mContext, OneWord mOneWord, boolean testMode) {
		this.mContext = mContext;
		this.mOneWord = mOneWord;
		this.testMode = testMode;
		if (mOneWord != null) {
			showAffix = mOneWord.showAffix(mContext);
			showSameShape = mOneWord.showSameShape(mContext, testMode);
		} else {
			MobclickAgentData.exception(mContext, "mOneWord为空指针");
		}
	}

	public boolean isShowAffix() {
		return showAffix;
	}

	public void setShowAffix(boolean showAffix) {
		this.showAffix = showAffix;
	}

	public boolean isShowSameShape() {
		return showSameShape;
	}

	public void setShowSameShape(boolean showSameShape) {
		this.showSameShape = showSameShape;
	}

	public int getInitPageIndex() {
		return initPageIndex;
	}

	public void setInitPageIndex(int initPageIndex) {
		this.initPageIndex = initPageIndex;
	}

	@Override
	public int getCount() {
		return 1 + (showAffix ? 1 : 0) + (showSameShape ? 1 : 0);// Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = init(container, position);
		return view;
	}

	private View init(ViewGroup container, int position) {

		View view = null;

		switch (position) {
		case 0:
			view = LayoutInflater.from(mContext).inflate(R.layout.view_word_inner_relationship, null);
			// initViewForRelationship(view);
			break;
		case 1:
			if (showAffix) {
				view = LayoutInflater.from(mContext).inflate(R.layout.view_word_inner_affix, null);
				// initViewForAffix(view);
			} else if (showSameShape) {
				view = LayoutInflater.from(mContext).inflate(R.layout.view_word_inner_sameshape, null);
				// initViewForSameShape(view);
			}
			break;
		case 2:
			view = LayoutInflater.from(mContext).inflate(R.layout.view_word_inner_sameshape, null);
			// initViewForSameShape(view);
			break;
		}

		TextView wordCn = (TextView) view.findViewById(R.id.word_inner_cn);
		wordCn.setText(mOneWord.getSensesSenior());

		// ViewParent viewParent = view.getParent();
		// if(viewParent!=null){
		// ViewGroup parent = (ViewGroup) viewParent;
		// parent.removeView(view);
		// }
		// container.addView(view);

		((ViewGroup) container).addView(view);
		views[position] = view;

		if (position == initPageIndex)
			initData(position);
		return view;
	}

	public void initData(int position) {
		switch (position) {
		case 0:
			initViewForRelationship(views[position]);
			break;
		case 1:
			if (showAffix) {
				initViewForAffix(views[position]);
			} else if (showSameShape) {
				initViewForSameShape(views[position]);
			}
			break;
		case 2:
			initViewForSameShape(views[position]);
			break;
		}
	}

	private void initViewForRelationship(View view) {
		if (WordFactory.getInstance(mContext).getNumOfRelationshipOfWord(mOneWord.getIndex_word_id()) <= 0)
			view.findViewById(R.id.relation_divider).setVisibility(View.GONE);
		else {
			LinearLayout relationshipContainer = (LinearLayout) view.findViewById(R.id.word_inner_relation_ship_item_container);
			CreatView.reLoadRelationShip(relationshipContainer, mOneWord, mContext);
		}
	}

	private void initViewForAffix(View view) {

		if (mOneWord.getAff_content() != null && mOneWord.getAff_content().length() > 0) {
			TextView text_ji = (TextView) view.findViewById(R.id.word_inner_ji);
			text_ji.setVisibility(View.VISIBLE);
			text_ji.setText("[记]" + mOneWord.getAff_content());
		}

		// 设置词根词缀
		Affix affix = WordFactory.getInstance(mContext).getWordRoot(mOneWord.getIndex_word_id());
		if (affix != null) {
			TextView affixText = (TextView) view.findViewById(R.id.word_inner_affix_text);
			LinearLayout affixTextDetail = (LinearLayout) view.findViewById(R.id.word_inner_affix_detail);
			List<OneWord> wordList;

			if (mOneWord.getIndex_word_id() == -1)
				wordList = WordFactory.getInstance(mContext).getWordListOfAffix(affix.getAffix_id());
			else
				wordList = WordFactory.getInstance(mContext).getWordListOfAffix(affix.getAffix_id(), mOneWord.getIndex_word_id());

			affixText.setText(affix.getAffix() + "   " + affix.getAff_sense());

			if (wordList.size() > 0) {
				CreatView.reloadAffix(affixTextDetail, wordList, testMode, mContext, view.findViewById(R.id.affix_detail_tv),mOneWord.getLemma_type());
			} else {
				view.findViewById(R.id.affix_detail_tv).setVisibility(View.GONE);
			}

		} else {
			view.findViewById(R.id.affix_layout).setVisibility(View.GONE);
			view.findViewById(R.id.affix_divider).setVisibility(View.GONE);
		}

	}

	private void initViewForSameShape(View view) {
		List<OneWord> wordList = WordFactory.getInstance(mContext).getSameShape(mOneWord.getIndex_word_id(), mOneWord.getLemma_type(), testMode);

		if (wordList.size() > 0) {
			LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			LinearLayout sameShapeTextDetail = (LinearLayout) view.findViewById(R.id.word_inner_sameshape_detail);
			sameShapeTextDetail.removeAllViews();

			for (int i = 0; i < wordList.size(); i++) {
				OneWord word = wordList.get(i);
				View item = CreatView.createWordView(mInflater, word, mContext);
				// View item =
				// LayoutInflater.from(mContext).inflate(R.layout.view_affix_item,
				// null);
				// TextView affixEn = (TextView)
				// item.findViewById(R.id.affix_item_en);
				// TextView affixCn = (TextView)
				// item.findViewById(R.id.affix_item_cn);
				// affixEn.setText(word.getLemma());
				// affixCn.setText(word.getSensesSenior());
				// item.setPadding(0, 0, 0, 30);
				sameShapeTextDetail.addView(item);
			}
		} else {
			view.findViewById(R.id.sameshape_layout).setVisibility(View.GONE);
		}
	}

}
